package com.groupseven.happycampus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.CarBo;
import com.groupseven.happycampus.common.pojo.Car;
import com.groupseven.happycampus.common.pojo.Submit;
import com.groupseven.happycampus.common.result.LayuiPageVo;
import com.groupseven.happycampus.mapper.SubmitMapper;
import com.groupseven.happycampus.service.SubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("submitService")
public class SubmitServiceImpl implements SubmitService {
    @Autowired
    private SubmitMapper submitMapper;
    @Override
    public LayuiPageVo<Submit> submitPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<Submit> submits = submitMapper.selectByParams(params);

        // mybatis 的 分页数据结构
        PageInfo<Submit> submitPageInfo = new PageInfo<>(submits);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Submit> submitLayuiPageVo = new LayuiPageVo<>();
        submitLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        submitLayuiPageVo.setMsg("分页列表数据"); // 提示信息


        submitLayuiPageVo.setCount((int) submitPageInfo.getTotal()); // 总记录数


        submitLayuiPageVo.setData(submitPageInfo.getList());  // 分页的列表数据
        return submitLayuiPageVo;
    }

    @Override
    public int deleteSubmit(Integer id) {
        return submitMapper.deleteSubmit(id);
    }

    @Override
    public int deleteSubmitsByIds(String ids) {
        String[] arrId = ids.split(",");
        return submitMapper.deleteSubmitsByIds(arrId);
    }

    @Override
    public int addSubmit(Map<String, Object> params) {
        int i=submitMapper.addSubmit(params);
        return i;
    }

    @Override
    public int updateSubmit(Map<String, Object> params) {
        int i=submitMapper.updateSubmit(params);
        return i;
    }

    @Override
    public List<CarBo> submitExcelData(Submit submit) {
        // 从数据库查询出来的 是  po  实体类
        List<Submit> submits = submitMapper.selectSubmit(submit);
        // 要把 po 转为 bo
        List<CarBo> submitBos = submits.stream()
                .map((ss) -> {
                    CarBo bo = new CarBo();
                    bo.setPlace(ss.getPlace());
                    bo.setTime(ss.getTime());
                    bo.setType(ss.getType());
                    return bo;
                }).collect(Collectors.toList());

        return submitBos;
    }
}
